package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows
import org.apache.flink.streaming.api.windowing.time.Time

object Demo2SessionWindow {

  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val lineDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = lineDS
      .flatMap(_.split(","))
      .map((_, 1))

    /**
      * 指定时间没有数据过来开始计算
      * ProcessingTimeSessionWindows:处理时间的会话窗口
      * EventTimeSessionWindows： 事件时间的会话窗口
      *
      */

    kvDS
      .keyBy(_._1)
      .window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)))
      .sum(1)
      .print()

    env.execute()

  }

}
